package util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import static java.awt.RenderingHints.KEY_ANTIALIASING;
import static java.awt.RenderingHints.VALUE_ANTIALIAS_ON;

public class VerifyCodeUtils {
    //使用到Algerian字体，系统里没有的话需要安装字体，字体只显示大写，去掉了1,0,i,o,2,Z几个容易混淆的字符
    public static final String VERIFY_CODES = "3456789ABCDEFGHJKLMNPQRSTUVWXY";
    private static double myFunc(double x){
        return 8*Math.sin(x/20)+15;
    }
    /**
     * 获取验证码 ，返回流
     * @param w   验证码的宽
     * @param h   验证码的高
     * @param os  输出流，获得到response的out
     * @param verifyCode
     */
    public static void outputImage(int w, int h, OutputStream os, String verifyCode) throws IOException {
        Random random = new Random();
        BufferedImage image = new BufferedImage(w,h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 =  image.createGraphics();
        g2.setRenderingHint(KEY_ANTIALIASING,VALUE_ANTIALIAS_ON);//设置抗锯齿度
        g2.setColor(new Color(255, 255, 255, 100));//设置当前颜色
        g2.fillRect(0,0,w,h);//设置边框
        //干扰线
        g2.setColor(Color.blue);
        double startX = random.nextInt();
        for (int i = 0; i < w; i++) {
            int y = (int) myFunc(startX+i);
            g2.drawLine(i, y, i, y);
        }
        Color c = getRandColor(200, 250);
        int fontSize = h-4;
        Font font = new Font("Algerian", Font.ITALIC, fontSize);//设置字体
        g2.setFont(font);
        char[] chars = verifyCode.toCharArray();
        int verifySize = verifyCode.length();
        for(int i = 0; i < verifySize; i++){
            AffineTransform affine = new AffineTransform();
            //旋转变化
            affine.setToRotation(Math.PI / 4 * random.nextDouble() * (random.nextBoolean() ? 1 : -1), (w / verifySize) * i + fontSize/2, h/2);
            g2.setTransform(affine);
            g2.setColor(getRandColor(100, 250));
            g2.drawChars(chars, i, 1, ((w-10) / verifySize) * i + 5, h/2 + fontSize/2 - 10);
        }

        g2.dispose();
        ImageIO.write(image, "jpg", os);
    }
    /**
     * 使用系统默认字符源生成验证码
     * @param verifySize    验证码长度
     * @return
     */
    public static String generateVerifyCode(int verifySize){
        return generateVerifyCode(verifySize, VERIFY_CODES);
    }
    /**
     * 使用指定源生成验证码
     * @param verifySize    验证码长度
     * @param sources   验证码字符源
     * @return
     */
    public static String generateVerifyCode(int verifySize, String sources){
        if(sources == null || sources.length() == 0){
            sources = VERIFY_CODES;
        }
        int codesLen = sources.length();
        Random rand = new Random(System.currentTimeMillis());
        StringBuilder verifyCode = new StringBuilder(verifySize);
        for(int i = 0; i < verifySize; i++){
            verifyCode.append(sources.charAt(rand.nextInt(codesLen-1)));
        }
        return verifyCode.toString();
    }
    private static int getRandomIntColor() {
        int[] rgb = getRandomRgb();
        int color = 0;
        for (int c : rgb) {
            color = color << 8;
            color = color | c;
        }
        return color;
    }

    private static Color getRandColor(int fc, int bc) {
        Random random = new Random();
        if (fc > 255)
            fc = 255;
        if (bc > 255)
            bc = 255;
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

    private static int[] getRandomRgb() {
        Random random = new Random();
        int[] rgb = new int[3];
        for (int i = 0; i < 3; i++) {
            rgb[i] = random.nextInt(255);
        }
        return rgb;
    }

}
